package controller.v2;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import model.LogTicketVO;
import model.MemberLevelItemVO;
import model.MemberTicketVO;
import model.MemberVO;
import service.v2.ConfigService;
import service.v2.DicService;
import service.v2.MemberLevelItemService;
import service.v2.MemberService;
import service.v2.MemberTicketService;
import util.PackageUtil;
import util.PropertieUtil;

@RequestMapping("/v2/memberticket")
@Controller
public class MemberTicketController {
	@Autowired
	private MemberTicketService memberTicketService;
	
	@Autowired
	private MemberService memberService;
	
	@Autowired
	private MemberLevelItemService memberLevelItemService;
	
	@Autowired
	private ConfigService configService;

	@Autowired
	private DicService dicService;
	
	/**
	 * 兌換券構成
	 * @param token
	 * @param member_id
	 */
	@ResponseBody
	@RequestMapping(value="/memberTicketInfo",method=RequestMethod.GET)
	public Object memberTicketInfo(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, 
			HttpServletResponse response,MemberVO memberVO,MemberTicketVO memberTicketVO)throws Exception{
		
		Map<String, Object> map = new HashMap<String, Object>();
		String host = configService.loadConfig("HOST");
		String version=configService.loadConfig("VERSION");
		//查詢可流通兌換券
		int use_tickets=memberTicketService.selectUseTickets(member_id);
 		map.put("use_tickets", use_tickets==0?"0":use_tickets+"");
		
		//查詢流通兌換券構成
		List<MemberTicketVO> ticket_kind =  memberTicketService.selectTicketKind(member_id);
		
		map.put("ticket_kind",ticket_kind);
		//不可流通的兌換券
		int disabled_tickets=memberTicketService.selectDisabledTickets(member_id);
		map.put("disabled_tickets", disabled_tickets==0?"0":disabled_tickets+"");
		
		//即将不可流通的兑换券
		String tickets_t = memberTicketService.selectTicketsT(member_id);
		map.put("tickets_t", tickets_t);
		map.put("memberticketsdisabledinfo_url",host+version+"/memberticket/memberTicketsDisabledInfo");//不可流通兑换券网页跳转链接
		map.put("ticketslog_url",host+version+"/logticket/ticketsLog");//兑换券明细
		//map.put("ticketslog_url","http://10.0.0.68:8080/v2/logticket/ticketsLog");
		return PackageUtil.packageParams(200,"successs",map,""); 
	}
	/**
	 * 即将转为不可流通的兑换券
	 * @param token
	 */
	@RequestMapping(value="/memberTicketsDisabledInfo",method=RequestMethod.GET)
	public Object memberTicketsDisabledInfo(@RequestHeader(value = "LANG", required = false) String lang,
			@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, 
			HttpServletResponse response,MemberVO memberVO,MemberTicketVO memberTicketVO)throws Exception{
		
		String host = configService.loadConfig("HOST");
		String ticket_name = configService.loadConfig("TICKET_NAME");
		
		//即将不可流通的兑换券
		String tickets_t = memberTicketService.selectTicketsT(member_id);
		//不可流通兑换券构成
		List<MemberTicketVO> tickets_disabledInfo =  memberTicketService.selectTicketsDisabledInfo(member_id,lang);
		
		if(lang ==null){
			request.setAttribute("tickets_disabledInfo", tickets_disabledInfo);
			request.setAttribute("tickets_t", tickets_t);
			//兑换券名称从数据库查出来。
			request.setAttribute("ticket_name",ticket_name);
			request.setAttribute("host_url",host);
			
			return "/app/ticket/memberTicketsDisabledInfo"; 
		}else{
			response.setContentType("text/json;charset=utf-8");   
			PrintWriter out = response.getWriter(); 
			
			Map map  =  new HashMap();
			map.put("tickets_disabledInfo", tickets_disabledInfo);
	 		map.put("tickets_t", tickets_t);
	 		map.put("ticket_name",ticket_name);
	 	    map.put("host_url",host);
	 		
	        out.print(JSON.toJSONString(PackageUtil.packageParams(200, "success", map, "")));
	        out.flush();
	        out.close();
	      return null;
		}
	}
	/**
	 * 兌換券充值
	 * @param token
	 * @param member_id
	 */
	@ResponseBody
	@RequestMapping(value="/memberTicketRecharge",method=RequestMethod.GET)
	public Object memberTicketRecharge(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response,MemberVO memberVO,
			MemberTicketVO memberTicketVO)throws Exception{
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		memberVO = memberService.seleceMemerSomeInfo(member_id);
		
		map.put("memberLevel",memberVO.getLevel());
		map.put("member_id",memberVO.getMember_id());
		map.put("member_rate",memberVO.getRate());
		map.put("level_name",memberVO.getLevel_name());
		map.put("member_start_ticket",memberVO.getStart_ticket());
		map.put("ticket_buy_maxnum",configService.loadConfig("TICKET_BUY_MAXNUM"));
		map.put("member_freeCash",memberVO.getFree_cash());
		map.put("rate_usd",configService.loadConfig("RATE_DOLLAR"));
		
		return PackageUtil.packageParams(200,"successs",map,""); 
	}
	/**
	 * 选择充值优惠
	 * @param token
	 * @param member_id
	 */
	@ResponseBody
	@RequestMapping(value="/ChoiceMemberTicketRecharge",method=RequestMethod.GET)
	public Object ChoiceMemberTicketRecharge(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,
			HttpServletRequest request, HttpServletResponse response,MemberVO memberVO,MemberTicketVO memberTicketVO)throws Exception{
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		String host = configService.loadConfig("HOST");
		String version=configService.loadConfig("VERSION");
		
		memberVO = memberService.seleceMemerSomeInfo(member_id);

		map.put("member_rate",memberVO.getRate());
		//可享受充值优惠
		List<MemberLevelItemVO> enjoy_privilege = memberLevelItemService.selectenjoyPrivilege(memberVO.getLevel());
		map.put("enjoy_privilege",enjoy_privilege);
		//进阶充值优惠
		List<MemberLevelItemVO> advance_privilege = memberLevelItemService.selectadvancePrivilege(memberVO.getLevel(),memberVO.getMember_id());
		map.put("advance_privilege",advance_privilege);
		map.put("membershipupgrade_url",host+version+"/memberticket/MembershipUpgrade");
		//map.put("membershipupgrade_url","http://10.0.0.68:8080/v2/memberticket/MembershipUpgrade");
		return PackageUtil.packageParams(200,"successs",map,""); 
	}
	
	/**
	 *兑换券 会员升级
	 * @param token
	 * @param member_id
	 */
	@RequestMapping(value="/MembershipUpgrade",method=RequestMethod.GET)
	public String MembershipUpgrade(@RequestHeader(value = "LANG", required = false) String lang,@RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, 
			HttpServletResponse response,MemberVO memberVO,MemberTicketVO memberTicketVO) throws Exception{
		if(os != null){
			os = os.toLowerCase();
		}	
		memberVO = memberService.seleceMemerSomeInfo(member_id);
		String host = configService.loadConfig("HOST");
		String version=configService.loadConfig("VERSION");
		String ticket_name=configService.loadConfig("TICKET_NAME");
		String app_name=configService.loadConfig("APP_NAME");
		
		response.setContentType("text/json;charset=utf-8");   
		PrintWriter out = response.getWriter(); 
		
		if(!"127".equals(memberVO.getLevel()) && !"126".equals(memberVO.getLevel())){
			
			//可享受充值优惠
			List<MemberLevelItemVO> enjoy_privilege = memberLevelItemService.selectenjoyPrivilege(memberVO.getLevel());
			//进阶充值优惠
			List<MemberLevelItemVO> advance_privilege = memberLevelItemService.selectadvancePrivilege(memberVO.getLevel(),memberVO.getMember_id());
			String masterName = dicService.getNameById(PropertieUtil.loadPropertie("master_level"));
			if(lang==null){
				request.setAttribute("member_level",memberVO.getLevel());
				request.setAttribute("enjoy_privilege",enjoy_privilege);
				request.setAttribute("advance_privilege",advance_privilege);
				request.setAttribute("os",os);
				request.setAttribute("ticket_name",ticket_name);
				request.setAttribute("host_url",host);
				request.setAttribute("masterName",masterName);
				request.setAttribute("registerProtocol_url",host+version+"/page/registerProtocol");
				//request.setAttribute("registerProtocol_url","http://10.0.0.68:8080/v2/page/registerProtocol");
				request.setAttribute("app_name",app_name);
				request.setAttribute("memberLevelup",host+version+"/member/memberLevelup");
				//request.setAttribute("memberLevelup","http://10.0.0.68:8080/v2/member/memberLevelup");
				return "/app/ticket/membershipUpgrade"; 
			}else{
				String rate_dollar=configService.loadConfig("RATE_DOLLAR");	
				Map map = new HashMap();
				
				map.put("member_level",memberVO.getLevel());
				map.put("enjoy_privilege",enjoy_privilege);
				map.put("advance_privilege",advance_privilege);
				map.put("os",os);
				map.put("ticket_name",ticket_name);
				map.put("host_url",host);
				map.put("masterName",masterName);
				map.put("registerProtocol_url",host+version+"/page/registerProtocol");
				map.put("app_name",app_name);
				map.put("memberLevelup",host+version+"/member/memberLevelup");
				map.put("rate_dollar",rate_dollar);

		        out.print(JSON.toJSONString(PackageUtil.packageParams(200, "success", map, "")));
		        out.flush();
		        return null;	
			}
		}else{
			
			out.print(JSON.toJSONString(PackageUtil.packageParams(403,"forbidden","","")) );
			out.flush();
			out.close();
			return null;
		}
	}
}
